package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.DynamicMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@RequestMapping("/dynamicMenu")
@RestController
@Slf4j
public class DynamicMenuController {

    @Reference
    private DynamicMenuService dynamicMenuService;

    @GetMapping("/findMenuByUser")
    public Result findMenuByUser(){
        log.info("[动态展示权限菜单]");
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof org.springframework.security.core.userdetails.User) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
            LinkedHashSet<Menu> menus = dynamicMenuService.findMenuByUser(user.getUsername());
            //ArrayList<Menu> menuArrayList = new ArrayList<>(menus);
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS,menus);
        }
        return new Result(false,MessageConst.GET_MENU_FAIL);
    }

}
